package com.qdsgjsfk.vision.ui.dialog;

import android.app.Dialog;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Adapter;
import android.widget.EditText;
import android.widget.TextView;

import com.qdsgjsfk.vision.R;
import com.qdsgjsfk.vision.base.BaseDialogFragment;
import com.rest.business.RestProxy;
import com.rest.response.BaseResponse;
import com.rest.response.YearResponse;

import java.util.ArrayList;
import java.util.List;

import io.reactivex.Observer;
import io.reactivex.disposables.Disposable;


/**
 * 选择任务，年份dialog
 *
 * @time 2020/9/25 11:31
 */

public class YearDialog extends BaseDialogFragment {

    private boolean isShowClear, isFromList;
    private MyAdapter myAdapter;

    public static YearDialog newInstance() {
        Bundle args = new Bundle();
        YearDialog fragment = new YearDialog();
        fragment.setArguments( args );
        return fragment;
    }

    private Dialog mDialog;
    private OnItemClickListener mListener;
    private TextView btn_confirm, btn_clear;
    private EditText edt_reason;
    private int type;
    private RecyclerView recyclerView;
    private List<String> yearList = new ArrayList<>();


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view = inflater.inflate( R.layout.dialog_year, container );
//        btn_confirm = view.findViewById( R.id.btn_confirm );
//
//        btn_confirm.setOnClickListener( new View.OnClickListener() {
//            @Override
//            public void onClick(View v) {
//                if (TextUtils.isEmpty( edt_reason.getText().toString().trim() )) {
//                    return;
//                }
//                mListener.onConfirm( edt_reason.getText().toString().trim(), type );
//            }
//        } );

        recyclerView = view.findViewById( R.id.recyclerView );
        GridLayoutManager manager = new GridLayoutManager( getActivity(), 4 );

        recyclerView.setLayoutManager( manager );
        myAdapter = new MyAdapter();
        recyclerView.setAdapter( myAdapter );

        return view;
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate( savedInstanceState );
        getPlanYearList();
    }


    @Override
    @NonNull
    public Dialog onCreateDialog(Bundle savedInstanceState) {
        mDialog = super.onCreateDialog( savedInstanceState );
        mDialog.setCancelable( false );
        Window window = mDialog.getWindow();
        if (window != null) {
            window.setGravity( Gravity.CENTER );
//            window.setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
//            window.setLayout(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        }
        return mDialog;
    }


    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        getDialog().getWindow().requestFeature( Window.FEATURE_NO_TITLE );
        super.onActivityCreated( savedInstanceState );
        getDialog().getWindow().setBackgroundDrawable( new ColorDrawable( 0x00000000 ) );
        getDialog().getWindow().setLayout( WindowManager.LayoutParams.MATCH_PARENT, WindowManager.LayoutParams.WRAP_CONTENT );
    }

    public OnItemClickListener getmListener() {
        return mListener;
    }

    public void setmListener(OnItemClickListener mListener) {
        this.mListener = mListener;
    }


    public interface OnItemClickListener {

        void onConfirm(String planYear);

//        void onCancel();
    }

    public boolean isShowing() {
        return mDialog != null && mDialog.isShowing();
    }

    class MyAdapter extends RecyclerView.Adapter<MyAdapter.RemarkHolder> {

        @Override
        public MyAdapter.RemarkHolder onCreateViewHolder(ViewGroup parent, int viewType) {
            return new RemarkHolder( getLayoutInflater().inflate( R.layout.item_remark, parent, false ) );
        }

        @Override
        public void onBindViewHolder(MyAdapter.RemarkHolder holder, final int position) {
            holder.tv_name.setText( yearList.get( position ) );
            holder.tv_name.setOnClickListener( new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    dismiss();
                    mListener.onConfirm( yearList.get( position ) );
                }
            } );
        }

        @Override
        public int getItemCount() {
            return yearList.size();
        }


        class RemarkHolder extends RecyclerView.ViewHolder {
            TextView tv_name;

            public RemarkHolder(View itemView) {
                super( itemView );
                tv_name = itemView.findViewById( R.id.tv_name );
            }
        }
    }

    private void getPlanYearList() {
        RestProxy.getInstance().getPlanYearList( new Observer<YearResponse>() {
            @Override
            public void onSubscribe(Disposable d) {

            }

            @Override
            public void onNext(YearResponse baseResponse) {
                yearList.clear();
                yearList.addAll( baseResponse.data );
                myAdapter.notifyDataSetChanged();
            }

            @Override
            public void onError(Throwable e) {

            }

            @Override
            public void onComplete() {

            }
        } );

    }


}
